<?php


namespace app\api\controller;


use app\api\model\OrderItem;

class Order extends BaseController
{


    /**
     *  立即购买的商品信息
     */
    public function getGoods()
    {
        $user_id = session('user_id');
        $goodsinfo = $this->request->param();
        $res = db('goods')->where('user_id', $user_id)->where('id', $goodsinfo['id'])->find();
        $total = $res['price'] * $goodsinfo['num'];
    }

    /**
     * 购买前判断该规格商品库存
     *
     * @return array|\think\response\Json
     */

    public function specStock()
    {
        //拿到规格表里面的商品信息
        $user_id = session('user_id');
        $user_id = 1;
        $model = new OrderItem();
        $goods_sku_id = 2;
        $res = $model->getStock($goods_sku_id);
        return $res;
    }

    /**
     * 订单编号生成
     *
     */
    public function orderNo()
    {
        $osn = date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
        return $osn;
    }

    /**
     * 立即购买
     *
     */
    public function buyNow()
    {
        //点击立即购买需要先获取到商品id,商品主图id,商品规格id {商品的规格属性,商品该规格的价格，库存 }
        $goods_id = $this->request->param('goods_id');
        

        //     $user_id = 1;
        //     //库存>0 将商品信息加入订单表
        //     $list = $this->specStock();
        //     // if ($list <= 0) {
        //     //     return $this->renderError('库存不足');
        //     // } else {
        //     $items = new OrderItem();
        //     $goods_id = 2;
        //     $image_id = 1;
        //
        //     $lists = [
        //         'order_number' => $this->orderNo(),
        //         'goods_id' => $list['goods_id'],
        //         'goods_sku_id' => $list['goods_sku_id'],
        //         'goods_number' => $list['goods_number'],
        //         'goods_price' => $list['goods_price'],
        //         'points_bonus' => $list['points_bonus'],
        //         'image_id' => $list['image_id'],
        //         'total_price' => $list['total_price'],
        //     ];
        //     $item = $items->joinOrderItem($lists);
        //     if ($item > 0) {
        //         $res = new OrderModel();
        //         $data = [
        //             'user_id' => $user_id,
        //             'order_no' => $this->orderNo(),
        //             'total_price' => $list['goods_number'] * $list['goodsspecs']['pro_price'],
        //             'order_price' => $list['goods_number'] * $list['goodsspecs']['pro_price'],
        //             'pay_price' => $list['goods_number'] * $list['goodsspecs']['pro_price'],
        //             'pay_status' => 10,
        //             'order_status' => 10,
        //         ];
        //         $re = $res->joinOrder($data);
        //         return $this->renderSuccess(compact('re'));
        //     } else {
        //         return $this->renderError(compact('item'), '立即购买出错');
        //     }
        // }
        // // }
    }


}